package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.request.transfer.TransferLocationReq;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.LocationResponse;
import com.wzdigit.wms.basic.client.response.transfer.SnLocationResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.transfer.TransferLocationService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName TransferLocationController
 * @Description 在库模块-移库
 * @Author LHR-PC
 * @Date 2021/3/22 10:00
 * @Version
 **/

@RestController
@Api(tags = "在库模块-移库")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class TransferLocationController {

    private static final String REQUEST_MODEL = "/transferLocation";

    @Autowired
    private TransferLocationService transferLocationService;

    @ApiOperation(value = "扫描条码", notes = "扫描条码")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/list")
    public SnLocationResponse list(@ApiParam(required = true, value = "条码") @RequestParam("sn") String sn,
                                   @ApiParam(required = true, value = "类型：单台0/整版1") @RequestParam("scanType") Integer scanType,
                                   HttpServletRequest servletRequest,
                                   HttpServletResponse servletResponse) {
        try {
            return transferLocationService.scanSn(sn, scanType);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "提交", notes = "提交")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/commit")
    public Result<?> commit(@RequestBody TransferLocationReq transferLocationReq,
                            HttpServletRequest servletRequest,
                            HttpServletResponse servletResponse) throws Exception {
        try {
            return transferLocationService.commit(transferLocationReq, HttpSessionUtil.getLoginName(servletRequest));
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "扫描目标库位", notes = "扫描目标库位")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/scanTargetLoc/{locNo}")
    public LocationResponse scanTargetLoc(@PathVariable String locNo,
                                          HttpServletRequest servletRequest,
                                          HttpServletResponse servletResponse) {
        try {
            return transferLocationService.scanTargetLoc(locNo);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}
